package person.zhaiming.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import person.zhaiming.commons.CommonResult;
import person.zhaiming.commons.entities.Payment;

import javax.annotation.Resource;

/**
 * @program: zhaimingSpringCloud
 * @description:
 * @author: zhaiming
 * @create: 2020-11-30 16:01
 **/
@RestController
@Slf4j
public class OrderController {

    //private static final String REQUEST_URL = "http://localhost:8001";
    private static final String REQUEST_URL = "http://consul-provider-payment";
    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value="/consumer/payment/create")
    public CommonResult creat(@RequestBody(required=false) Payment payment){
        return restTemplate.postForObject(REQUEST_URL+"/payment/create", payment, CommonResult.class);
    }

    @GetMapping(value="/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        return restTemplate.getForObject(REQUEST_URL + "/payment/get/" + id, CommonResult.class);
    }

    @GetMapping(value="/consumer/payment/getForEntity/{id}")
    public CommonResult<Payment> getPaymentById2(@PathVariable("id") Long id){
        ResponseEntity<CommonResult> res = restTemplate.getForEntity(REQUEST_URL + "/payment/get/" + id, CommonResult.class);
        if(res != null && res.getStatusCode().is2xxSuccessful()){
            return res.getBody();
        }else{
            return new CommonResult(500, "未正常获得值");
        }
    }

    @GetMapping(value="/consumer/payment/consul")
    public Object getZK(){
        return restTemplate.getForObject(REQUEST_URL + "/payment/consul", String.class);
    }
}
